"use strict";
(function (global, factory) {
    // @ts-ignore
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : // @ts-ignore
        typeof define === 'function' && define.amd ? define(factory) : // @ts-ignore
            (global = global || self, global.Wmc = factory());
}(this, function () {
    const ToSystem = 'system'; // 系统消息
    const ToGlobal = 'global'; // 广播消息
    const TypeJoin = "join"; // 加入房间
    const TypeLeave = "leave"; // 离开房间
    const TypeRegister = "register"; // 注册客户端身份
    const TypeRoom = "room"; // 房间类消息
    const TypePersonal = "personal"; // 个人类消息
    const TypeGroup = "group"; // 用户组类消息
    function Wmc(options) {
        if (!window['WebSocket']) {
            throw new Error('Your browser does not support WebSocket');
        }
        // 准备发送数据包
        const preparePayload = function (to, type, message) {
            return JSON.stringify({
                to: to,
                type: type,
                content: message
            });
        };
        const ws = new WebSocket(options.url, options.schema);
        const onOpen = function (ev) {
            if (options.open) {
                options.open(ev);
            }
        };
        const onClose = function (ev) {
            if (options.close) {
                options.close(ev);
            }
        };
        const onError = function (ev) {
            if (options.error) {
                options.error(ev);
            }
        };
        const onMessage = function (ev) {
            if (options.message) {
                const contents = ev.data.split(String.fromCharCode(1));
                contents.forEach((content) => {
                    options.message(ev, content);
                });
            }
        };
        ws.onopen = onOpen;
        ws.onclose = onClose;
        ws.onerror = onError;
        ws.onmessage = onMessage;
        return {
            ws: ws,
            close: function (code, reason) {
                ws.close(code, reason);
            },
            join: function (roomId) {
                ws.send(preparePayload(ToSystem, TypeJoin, roomId));
            },
            leave: function (roomId) {
                ws.send(preparePayload(ToSystem, TypeLeave, roomId));
            },
            register: function (userId) {
                ws.send(preparePayload(ToSystem, TypeRegister, userId));
            },
            send: function (to, type, message) {
                ws.send(preparePayload(to, type, message));
            },
            broadcast: function (message) {
                ws.send(preparePayload(ToGlobal, "", message));
            },
            sendRaw: function (message) {
                ws.send(message);
            }
        };
    }
    Wmc.ToSystem = ToSystem; // 系统消息
    Wmc.ToGlobal = ToGlobal; // 广播消息
    Wmc.TypeJoin = TypeJoin; // 加入房间
    Wmc.TypeLeave = TypeLeave; // 离开房间
    Wmc.TypeRegister = TypeRegister; // 注册客户端身份
    Wmc.TypeRoom = TypeRoom; // 房间类消息
    Wmc.TypePersonal = TypePersonal; // 个人类消息
    Wmc.TypeGroup = TypeGroup; // 用户组类消息
    return Wmc;
}));
